package com.nxg.pm04.baseAdpater;

import android.app.Activity;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nxg.pm04.R;

import java.util.List;

public class AddBaseAdapter extends BaseAdapter {
    Activity activity;//活动窗口
    List<String> list;
    TextView textView;//文本视图
    //有参构造方法
    public AddBaseAdapter(Activity activity, List<String> list) {
        this.activity = activity;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
    /**
     *
     * @param position int i需要被final修饰
     * @param convertView
     * @param parent
     * @return
     */
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        convertView = LayoutInflater.from(activity).inflate(R.layout.list,null);

        try{
            //R.layout.list1  布局
            textView = convertView.findViewById(R.id.tvList);
            //list1.xml中的取消按钮
            ImageView  cancelImg = convertView.findViewById(R.id.cancelImg);
            textView.setText(list.get(position));
            //对图片按钮 设置点击监听事件
            cancelImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //移除列表第i行数据
                    list.remove(position);
                    // 执行完毕，更新UI
                    AddBaseAdapter.this.notifyDataSetChanged();
                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }
        return convertView;
    }
}
